﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data;
using VAdvantage.DataBase;
using System.ComponentModel;
using System.Collections.ObjectModel;
using VAdvantage.Utility;
using System.Windows.Data;
using VAdvantage.Classes;

namespace VAdvantage.Dialogs
{
    public partial class UserDialog : ChildWindow
    {
       
        public UserDialog()
        {
            InitializeComponent();
            txtEmpty.Text = Msg.GetMsg("NoDataFound");
            Init();

        }
        ObservableCollection<UserInfo> taskList = new ObservableCollection<UserInfo>();
        private void Init()
        {
            string sql = @"SELECT   AD_User_ID ,
                                    Name,
                                    IsLoginUser 
                                    FROM AD_User 
                           WHERE    AD_USER_ID>100 
                                AND AD_CLIENT_ID>0 
                                AND ISLOGINUSER='Y'";
            DataSet ds = new DataSet();
            try
            {
                 System.Threading.ThreadPool.QueueUserWorkItem(delegate
                 {
                     ds=DB.ExecuteDataset(sql,null);
                     Dispatcher.BeginInvoke(delegate
                     {
                         if (ds.Tables[0].Rows.Count > 0)
                         {
                             taskList.Clear();
                             txtEmpty.Visibility = Visibility.Collapsed;
                             UserInfo user=null;
                             for(int i=0;i<ds.Tables[0].Rows.Count;i++)
                             {
                                 user = new UserInfo();
                                 user.AD_User_ID = Convert.ToInt32(ds.Tables[0].Rows[i]["AD_User_ID"]);
                                 user.Name = ds.Tables[0].Rows[i]["Name"].ToString();
                                 user.IsLoginUser = ds.Tables[0].Rows[i]["IsLoginUser"].ToString() == "Y" ? true : false;
                                 taskList.Add(user);
                             }
                         }
                         else
                         {
                             txtEmpty.Visibility = Visibility.Visible;
                         }
                         PagedCollectionView taskListView = new PagedCollectionView(taskList);
                         vdgdata.AutoGenerateColumns = false;
                         vdgdata.ItemsSource = taskListView;
                     });
                 });          
                
               
            }
            catch
            {
            }
        }

        private void SaveData()
        {
            if (taskList.Count <= 0)
            {
                return;
            }
            char isLoginUser='N';
            int count = 0;
            OKButton.IsEnabled=false;
            System.Threading.ThreadPool.QueueUserWorkItem(delegate
            {
                try
                {
                    for (int i = 0; i < taskList.Count; i++)
                    {
                        isLoginUser = taskList[i].IsLoginUser == true ? 'Y' : 'N';
                        if (DB.ExecuteQuery("Update AD_User SET IsloginUser='" + isLoginUser + "' WHERE AD_User_ID=" + taskList[i].AD_User_ID) > 0)
                        {
                            count++;
                        }
                    }
                    Dispatcher.BeginInvoke(delegate
                    {
                        ShowMessage.Info(count.ToString() + " Rows Updated ", true, "", "");
                        OKButton.IsEnabled = true;
                    });
                }
                catch
                {
                   // ShowMessage.Info(count.ToString() + " Rows Updated ", true, "", "");
                    Dispatcher.BeginInvoke(delegate
                    {
                        ShowMessage.Info(count.ToString() + " Rows Updated ", true, "", "");
                        OKButton.IsEnabled = true;
                    });
                 
                }
            });
           
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            SaveData();
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }

    public class UserInfo : System.ComponentModel.INotifyPropertyChanged
    {
        // The Task class implements INotifyPropertyChanged so that 
        // the datagrid row will be notified of changes to the data
        // that are made in the row details section.

        // Private task data.
        private string name;

        private int userID;
        bool isloginUser;

        public int AD_User_ID
        {
            get { return this.userID; }
            set
            {
                if (value != this.userID)
                {
                    this.userID = value;
                    NotifyPropertyChanged("AD_User_ID");
                }
            }
        }
        public string Name
        {
            get { return this.name; }
            set
            {
                this.name = value;
                NotifyPropertyChanged("Name");

            }
        }

        public bool IsLoginUser
        {
            get { return this.isloginUser; }
            set
            {
                this.isloginUser = value;
                NotifyPropertyChanged("IsLoginUser");
            }

        }
         // Implement INotifyPropertyChanged interface.
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {

                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

